package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.oneconnect.OneConnectRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;

/**
 *
 * 壹账通-好信场景风险指数
 *
 * @author renzhanzheng
 */

public class OneConnectScenesriskParse extends ParseBase  {
    private Text val = new Text();
    private Text key = new Text();

    /**
     *
     * 壹账通-好信场景风险指数
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        OneConnectRecord obj = (OneConnectRecord) model;
        if (!("." + obj.getServiceName()).equals(getSubType())) {
            return false;
        }
        Date date = new Date(obj.getTimestamp());
        if (date.before(DateUtil.formateToDate("2020-07-22 19:00:00"))) {
            return false;
        }

        JSONObject info = obj.getData() == null ? new JSONObject() : JSON.parseObject(obj.getData());

        StringBuilder sb = getBaseInfo(obj, successful);

        //错误信息
        sb.append(info.getString("erMsg")).append(CRT001);
        //序列号
        sb.append(info.getString("seqNo")).append(CRT001);
        //错误代码
        sb.append(info.getString("erCode")).append(CRT001);
        //综合评分
        sb.append(info.getString("credooScore")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", "").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getSubType() {
        return ".msc8286";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_oneconnect_scenesrisk_inc";
    }

    @Override
    public String getType() {
        return RecordType.ONE_CONNECT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "壹账通-好信场景风险指数";
    }
}
